# Cantuna Makefile
# For host-executable output, use:
#  make clean all
# For Caanoo output, use:
#  make TARGET=caanoo clean all


CFLAGS = -Os -Wall
LDFLAGS = -pthread
LIBS = -lSDL -lSDL_image -lSDL_ttf -lgstreamer-0.10 -lgstpbutils-0.10 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lgsttag-0.10 -lxml2 -lstdc++ -lgstbase-0.10

# Location of Caanoo SDK (can be used in host target in theory)
GPH_SDK = /usr/src/GPH_SDK
LIBDIR =
USR = /usr

SRCDIR = src
INC = -I$(PWD)/include

ifndef TARGET
# Use host system's toolchain
ARCH =
CFLAGS += -g
# Not using these libraries:
# -ldge20 -lGL -lEGL -lGLES_CM -lSDL_mixer -lsmpeg -ltiff -lSDLmain 

# Use this for strict SDK libraries
#LIBDIR += -L$(GPH_SDK)/DGE/lib/host -L$(GPH_SDK)/lib/host
#LDFLAGS += -Wl,-rpath,$(GPH_SDK)/DGE/lib/host -Wl,-rpath,$(GPH_SDK)/lib/host

LIBS += -lGL
OBJDIR = host/.objects
OUT = host/Cantuna

else
ifeq ($(TARGET),caanoo)
TOOLS = $(GPH_SDK)/tools/gcc-4.2.4-glibc-2.7-eabi
USR = $(TOOLS)/arm-gph-linux-gnueabi/sys-root/usr

ARCH = $(TOOLS)/bin/arm-gph-linux-gnueabi-

# Avoid using DGE libraries if possible
#INC += -I$(GPH_SDK)/DGE/include -I$(GPH_SDK)/DGE/include/SDL -I$(GPH_SDK)/include
#LIBDIR = -L$(GPH_SDK)/DGE/lib/target -L$(GPH_SDK)/lib/target

#CFLAGS += -Wfatal-errors
CFLAGS += -DCAANOO
# Not using these libraries:
# -ldge20 -lopengles_lite -lglport -lSDL_mixer -lpng -lz -lts -lsmpeg -lfreetype -lpthread
LIBS += -lopengles_lite -lglport

RUNLIB = \$$ORIGIN/../../libs

LDFLAGS += -s -Wl,-rpath,$(RUNLIB)/gstreamer-0.10 -Wl,-rpath,$(RUNLIB)/glib-2.0 -Wl,-rpath,$(RUNLIB)/libxml2
OBJDIR = caanoo/.objects
OUT = caanoo/Cantuna/Cantuna.gpe
endif
endif

INCROOT = $(USR)/include
ifneq ($(USR),/usr)
LIBDIR += -L$(USR)/lib
INC += -I$(INCROOT)
endif

INC += -I$(INCROOT)/gstreamer-0.10 -I$(INCROOT)/glib-2.0 -I$(INCROOT)/libxml2 -I$(USR)/lib/glib-2.0/include

# Not built yet: text.o
OBJ = $(OBJDIR)/main.o $(OBJDIR)/screen.o $(OBJDIR)/sound.o $(OBJDIR)/util.o $(OBJDIR)/control.o $(OBJDIR)/img.o

all: before $(OUT)

before: 
	@test -d `dirname $(OUT)` || mkdir -p `dirname $(OUT)`
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)

$(OUT): $(OBJ) Makefile
	$(ARCH)$(CC) $(LDFLAGS) $(LIBDIR) $(LIBS) -o $(OUT) $(OBJ) $(LIB)
	@echo
	@echo "Executable location : $(OUT)"

%.o: $(OBJDIR)/%.o

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
	$(ARCH)$(CXX) $(CFLAGS) $(INC) -c -o $@ $<

$(OBJDIR)/%.o: $(SRCDIR)/%.c
	$(ARCH)$(CXX) $(CFLAGS) $(INC) -c -o $@ $<

clean: 
	rm -f $(OBJ) $(OUT)
	rm -rf $(OBJDIR)

.PHONY: all before clean %.o

